const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 把时间字符串转换成时间戳
function timeNumberWithStr(timeStr) {
  // 毫秒级别
  let timestampInMilliseconds = Date.parse(timeStr);
  return timestampInMilliseconds
}
// 根据时间戳转换成天时分
function dateWithtimeNumber(timeNumber) {
  let days = Math.floor(timeNumber / (1000 * 60 * 60 * 24));
  let hours = Math.floor((timeNumber % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  let minutes = Math.floor((timeNumber % (1000 * 60 * 60)) / (1000 * 60));
  let seconds = Math.floor((timeNumber % (1000 * 60)) / 1000);
  return `${days}天  ${hours}时  ${minutes}分  ${seconds}秒`
}
// 获取年月日
function getCurrtDate() {
  let currentDate = new Date();
  let formattedDate = `${currentDate.getFullYear()}-${(currentDate.getMonth() + 1).toString().padStart(2, '0')}-${currentDate.getDate().toString().padStart(2, '0')}`;
  return formattedDate
}
// 将2024-10-26 09:00:00转换为 2024/10/26 09:00:00
function formatTimestamp(timestamp) {
  // 将时间戳字符串分割为日期和时间部分  
  if (timestamp) {
    const [dateStr, timeStr] = timestamp.split(' ');
    // 进一步分割日期和时间的各个部分  
    if (dateStr && timeStr) {
      const [year, month, day] = dateStr.split('-');
      const [hours, minutes, seconds] = timeStr.split(':');
       // 格式化为所需的字符串格式  
       const formattedTimestamp = `${year}/${month.padStart(2, '0')}/${day.padStart(2, '0')} ${hours.padStart(2, '0')}:${minutes.padStart(2, '0')}:${seconds.padStart(2, '0')}`;
       return formattedTimestamp;
    }
    
    return timestamp
  }
  return timestamp
}

// 通过，分割成数组
function getListArrWithStr(str, key) {
  if (!str) {
    return []
  }
  return str.split(key)
}
// 把带有null的字符串转换成空字符
function nullwithStr(str) {
  if (str == null || str == 'undefined' || !str) {
    return ''
  } else {
    return str
  }
}

module.exports = {
  formatTime: formatTime,
  timeNumberWithStr,
  dateWithtimeNumber,
  getCurrtDate,
  getListArrWithStr,
  nullwithStr,
  formatTimestamp
}
